package com.linghui.videoplus.ipai.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.linghui.videoplus.ipai.activity.MyVideoActivity;
import com.linghui.videoplus.ipai.api.http.HttpPostApi;
import com.linghui.videoplus.ipai.api.http.HttpPostUtil;
import com.linghui.videoplus.ipai.api.json.JsonAPI;
import com.linghui.videoplus.ipai.bean.VideoBean;
import com.linghui.videoplus.ipai.utils.Constants;

public class UploadThread implements Runnable {

	private Context mContext;
	private VideoBean video;
	private Handler handler;
	private String uid;
	private String author;
	private String title;
	private String desc;

	public UploadThread(Context mContext,VideoBean video, Handler handler,String uid,String author,String title,String desc) {
		this.mContext=mContext;
		this.video = video;
		this.handler = handler;
		this.uid=uid;
		this.author=author;
		this.title=title;
		this.desc=desc;

	}

	@Override
	public void run() {
		try {
			HttpPostUtil uapi = new HttpPostUtil(Constants.KANKAN_UPLOAD_ENTITY,handler);
			File f = new File(video.getVideoPath());
			uapi.addFileParameter("Filedata", f);
			byte[] b = uapi.send();
			String result = new String(b);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("uid", uid+"|"+author));
			params.add(new BasicNameValuePair("title", title));
			params.add(new BasicNameValuePair("memo", desc));
			params.add(new BasicNameValuePair("tags", ""));
			params.add(new BasicNameValuePair("fileName", result));
			params.add(new BasicNameValuePair("author", author));
			params.add(new BasicNameValuePair("webid", "1"));
			params.add(new BasicNameValuePair("webCols", "3234"));
			
     		result = 	HttpPostApi.sendPost(Constants.KANKAN_UPLOAD_INFO, params);
			sendMessage(Constants.EXTRA_UPLOAD_STATUS_OK);
			JsonAPI jsonApi = new JsonAPI();
			video.setUploadStatus(Constants.EXTRA_UPLOAD_STATUS_OK);
			jsonApi.updateMyVideo(mContext, video);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 发送消息
	 * @param percent
	 */
	private void sendMessage(int status){
		Message msg =handler.obtainMessage();
		msg.what = 1;
		msg.obj = status;
		handler.sendMessage(msg);
	}
}
